using System;
using UnityEngine;

/// <summary>
/// todo: 这个类是临时用的，后期要重构
/// </summary>
public class ActorStats : MonoBehaviour
{
    [Serializable]
    private class PreData
    {
        [field: SerializeField] public int Level { get; private set; }
        [field: SerializeField] public float Health { get; private set; }
        [field: SerializeField] public float Attack { get; private set; }
        [field: SerializeField] public float Defense { get; private set; }
        [field: SerializeField] public float MoveSpeed { get; private set; }
        [field: SerializeField] public float Hit { get; private set; }
        [field: SerializeField] public float Evasion { get; private set; }
        [field: SerializeField] public float Critical { get; private set; }
        [field: SerializeField] public ElementType Element { get; private set; }
    }

    [SerializeField] private PreData _preData;


    public int Level { get; private set; }

    private float _health;

    public float Health
    {
        get => _health;
        set
        {
            if (Mathf.Approximately(_health, value))
            {
                return;
            }

            _health = value;
            OnHealthChange?.Invoke();
        }
    }

    public Stat MaxHealth { get; private set; }

    public Stat Attack { get; private set; }

    public Stat Defense { get; private set; }

    public Stat MoveSpeed { get; private set; }

    public Stat Hit { get; private set; }

    public Stat Evasion { get; private set; }

    public Stat Critical { get; private set; }

    public Stat CriticalDamage { get; } = new(2f);

    public ElementType AttackElement { get; set; }
    public ElementType Element { get; set; }

    public event Action OnHealthChange;

    private void Awake()
    {
        Level = _preData.Level;
        MaxHealth = new Stat(_preData.Health);
        Attack = new Stat(_preData.Attack);
        Defense = new Stat(_preData.Defense);
        MoveSpeed = new Stat(_preData.MoveSpeed);
        Hit = new Stat(_preData.Hit);
        Evasion = new Stat(_preData.Evasion);
        Critical = new Stat(_preData.Critical);
        Element = _preData.Element;
    }

    private void Start()
    {
        Health = MaxHealth;
    }

    public void TakeDamage(float amount)
    {
        Health = Mathf.Clamp(Health - amount, 0f, MaxHealth);
    }

    public void TakeHeal(float amount)
    {
        Health = Mathf.Clamp(Health + amount, 0f, MaxHealth);
    }
}